const logTime = (name) => {
  console.log(`Log...${name} --->` + new Date().toLocaleDateString());
};

exports.callback = () => {
  setTimeout(() => {
    logTime("callback 1");
    setTimeout(() => {
      logTime("callback 2");
    }, 100);
  }, 100);
};

const promise = (name, delay = 100) =>
  new Promise((resolve) => {
    setTimeout(() => {
      resolve(logTime(name));
    }, delay);
  });

// exports.promise = () => {
//   promise('promise1')
//     .then(promise('promise2'))
//     .then(promise('promise3'))
// }

exports.generator = () => {
  const generator = function* (name) {
    yield promise(name + 1);
    yield promise(name + 2);
    yield promise(name + 3);
  };

  let co = (generator) => {
    generator.next().value?.then(co(generator));
  };

  co(generator("co-generator---->"));
};

exports.asyncAwait = async () => {
  await promise("async-await1");
  await promise("async-await2");
  await promise("async-await3");
  await promise("async-await4");
};

exports.event = async () => {
  const asyncFunc = (name) => (event) => {
    setTimeout(() => {
      logTime(name);
      event.emit("end");
    }, 100);
    return event;
  };
  const arry = [
    asyncFunc("event 1"),
    asyncFunc("event 2"),
    asyncFunc("event 3"),
    asyncFunc("event 4"),
  ];
  const { EventEmitter } = require("events");
  const event = new EventEmitter();
  let i = 0;
  event.on("end", () => i < arry.length && arry[i++](event));

  event.emit('end')
};
